package com.blast.cn.ui

import android.content.Intent
import android.view.KeyEvent
import androidx.recyclerview.widget.LinearLayoutManager
import com.blast.cn.R
import com.blast.cn.adapter.SettingsAdapter
import com.blast.cn.bean.SettingsItem
import com.wxl.common.AppConstant
import com.wxl.common.AppContext
import com.wxl.common.base.AbsActivity
import com.wxl.common.util.DateUtils
import com.wxl.common.util.ObservableUtil
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_settings_layout.*

class SettingsActivity : AbsActivity(){

     var adapter:SettingsAdapter?=null

    override fun getCreateContentViewId(): Int {
        return R.layout.activity_settings_layout
    }

    override fun onCreateViewChanged() {
        setTopTitle("系统设置")
        settingsView.layoutManager = LinearLayoutManager(this)
        adapter = SettingsAdapter(SettingsItem.getSettings())
        adapter?.bindToRecyclerView(settingsView)
        adapter?.setOnItemClickListener { adapter, view, position ->
            when (position) {
                2 -> {//操作手册
                }
                3 -> {//设备信息
                    startActivity(Intent(this,ControllInfoActivity::class.java))
                }
                4 -> {//软件版本
                    startActivity(Intent(this,SoftWareVersionActivity::class.java))
                }
                5 -> {//售后服务
                    startActivity(Intent(this,ServiceActivity::class.java))
                }
                6 -> {//更多
                    startActivity(Intent(this,MoreActivity::class.java))
                }
            }
        }
        adapter?.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.settingsItemSwitchLogin -> {//切换账号
                    startActivity(Intent(this,LoginActivity::class.java))
                }
                R.id.settingsItemUpdatePwd -> {//修改密码
                    if(AppContext.appContext.getLoginAcc() != null) {
                        startActivity(Intent(this, UpdatePwdActivity::class.java))
                    } else {
                        startActivity(Intent(this,LoginActivity::class.java))
                    }
                }
                R.id.settingsItemUploadCjqData -> {//上传采集器信息
                    startActivity(Intent(this,UploadCollectorVersionActivity::class.java))
                }
            }
        }
        startCountDownSurplusTime()
    }


    override fun onResume() {
        super.onResume()
        val settings = SettingsItem.getSettings()
        if(adapter != null){
            if(adapter!!.getSelectedPosition() >=0 && adapter?.data!!.size > adapter!!.getSelectedPosition()){
                settings[adapter!!.getSelectedPosition()].isSelect = true
            }
        }
        adapter?.setNewData(settings)
    }


    override fun onKeyDownEvent(keyCode: Int, event: KeyEvent?): Boolean {
        adapter?.onKeyDown(keyCode, event)
        if(keyCode == KeyEvent.KEYCODE_BACK){
            finish()
        }
        if(keyCode == KeyEvent.KEYCODE_ENTER){
            if(adapter != null){
                if(adapter!!.getSelectedPosition() >=0){
                    adapter!!.onItemClickListener?.onItemClick(adapter,settingsView,adapter!!.getSelectedPosition())
                }
            }
        }
        return true
    }

    /**
     * 超时倒计时
     */
    private fun startCountDownSurplusTime(){
        if(adapter == null) return
        if(!AppContext.appContext.checkLoginOutTime()){
            val time = System.currentTimeMillis() - AppContext.appContext.getLoginAccTime() - AppConstant.mLoginLimitTime
            ObservableUtil.tick(0,1000,time / 1000, Consumer {
                val t = time - it*1000
                if(adapter != null) {
                    if(t > 0){
                        adapter!!.data[1].timeSurplus = DateUtils.getYMDHMSTime(t)
                    } else {
                        adapter!!.data[1].timeSurplus = "已超时"
                    }
                    adapter!!.notifyItemChanged(1,"timeSurplus")
                }
            },null)
        }
    }
}